home *** CD-ROM | disk | FTP | other *** search
/ Aminet 23 / Aminet 23 (1998)(GTI - Schatztruhe)[!][Feb 1998].iso / Aminet / dev / c / fortify22.lha / TEST2.CXX < prev    next >
C/C++ Source or Header  |  1995-11-01  |  1KB  |  61 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "fortify.h"
  5.  
  6. /*
  7.  * NOTE: The Fortify routines will compile away to nothing
  8.  * if FORTIFY isn't defined in the makefile.
  9.  *
  10.  * DO NOT insert #define FORTIFY here. It Will Not Work.
  11.  * All files (including fortify.cxx) need FORTIFY to be
  12.  * defined. The correct place for this is the makefile.
  13.  */
  14.  
  15. class A
  16. {
  17. public:
  18.     ~A() { delete (char*)123; }
  19. };
  20.  
  21. int
  22. main(int argc, char **argv)
  23. {                   
  24.     char *foo;
  25.                    
  26.     Fortify_EnterScope();               
  27.  
  28.     /* zero size test */
  29.     foo =(char*) malloc(0);
  30.     printf("malloc(0) %s\n", foo ? "succeeded" : "failed");
  31.  
  32.     /* zero size test */
  33.     foo = new char[0];
  34.     printf("new char[0] %s\n", foo ? "succeeded" : "failed");
  35.  
  36.                    
  37.     foo = new char;
  38.  
  39.     /* note we use the incorrect deallocator */
  40.     /* note this will only be detected if FORTIFY_PROVIDE_ARRAY_NEW
  41.      * and FORTIFY_PROVIDE_ARRAY_DELETE are both turned on
  42.      * (and your compiler supports them) */
  43.     delete[] foo;
  44.     *foo = 'Z';
  45.  
  46.     foo = new char;
  47.     Fortify_LabelPointer(foo, "we use the wrong deallocator on this one");
  48.  
  49.     /* note we use the incorrect dealocator */
  50.     free(foo);
  51.  
  52.     /* the destructor of this class does an illegal delete -
  53.      * demonstrates the delete-stack
  54.      */
  55.     delete new A;
  56.  
  57.     Fortify_LeaveScope();
  58.  
  59.     return 1;
  60. }
  61.